package com.happy.study.controller;


import com.happy.study.entity.FollowEntity;
import com.happy.study.entity.UserEntity;
import com.happy.study.entity.bo.FollowBo;
import com.happy.study.servier.IFollowService;
import com.happy.study.util.ResultBody;
import com.happy.study.util.StateEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping ("/follow")
public class FollowController {

    @Resource
    private IFollowService iFollowService;

    @RequestMapping ("/addSingle")
    @ResponseBody
    public ResultBody<FollowEntity> addSingle ( FollowEntity followEntity, HttpSession session ) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        followEntity.setUserId ( userEntity.getId () );
        followEntity = iFollowService.addSingle ( followEntity );
        return ResultBody.success ( followEntity );
    }

    @RequestMapping ("/queryFriends")
    @ResponseBody
    public ResultBody<FollowEntity> queryFriends ( FollowEntity followEntity, HttpSession session ) {
        List<FollowBo> followEntities = iFollowService.queryList ( followEntity );
        return ResultBody.success ( followEntities );
    }

    @RequestMapping ("/deleteSingle")
    @ResponseBody
    public ResultBody<FollowEntity> deleteSingle ( FollowEntity followEntity, HttpSession session ) {
        UserEntity userEntity = (UserEntity) session.getAttribute ( "userEntity" );
        if ( userEntity == null ) {
            return ResultBody.fail ( StateEnum.REQUEST_POWER_EXCE );
        }
        followEntity.setUserId ( userEntity.getId () );
        followEntity = iFollowService.deleteSingle ( followEntity );
        return ResultBody.success ( followEntity );
    }

}
